package com.itheima.dao;

import com.itheima.dao.sqlprovider.CheckGroupProvider;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface CheckGroupDao {


    /**
     * 在新增数据的之后返回该条新增数据的自增id ==> @Options
     * @param checkGroup
     */
    @Insert("insert into t_checkgroup values(null,#{code},#{name},#{helpCode},#{sex},#{remark},#{attention})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    public void add(CheckGroup checkGroup);

    /**
     * t_checkgroup_chheckitem中间表维护
     * @param map
     */
    @Insert("insert into t_checkgroup_checkitem values(#{checkgroup_id},#{checkitem_id})")
    public void addCheckItemIdAndCheckGroupId(Map map);

    /**
     * 查询全部信息
     * @return
     */
    @Select("<script>"+
            "select * from t_checkgroup " +
            "<where>" +
            "<if test='value!=null'> " +
            "or code=#{value} " +
            "or name like concat('%',#{value},'%') " +
            "or helpCode=#{value} " +
            "</if>" +
            "</where>"+
            "</script>")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "code",property = "code"),
            @Result(column = "name",property = "name"),
            @Result(column = "helpCode",property = "helpCode"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "remark",property = "remark"),
            @Result(column = "attention",property = "attention"),
            @Result(property = "id",
                    column = "checkgroup_id",
                    javaType = List.class,
                    many=@Many(select = "com.itheima.dao.CheckGroupDao.findItemByGroupId")
            )
    })
    //@Select("select * from t_checkgroup")
    public List<CheckGroup> findAll(String value);

    /**
     * 中间表查询
     * @param checkGroupId
     * @return
     */
    @Select("select * from t_checkgroup_checkitem where checkgroup_id=#{checkgroup_id}")
    public List<CheckItem> findItemByGroupId(@Param("checkgroup_id") Integer checkGroupId);

    /**
     * 更新和删除检查组时看时机调用此操作
     * @param id
     * @return
     */
    @Delete("delete from t_checkgroup_checkitem where checkgroup_id=#{checkgroup_id}")
    public long deleteCheckGroupIdAndCheckCheckItemId(@Param("checkgroup_id") Integer id);

    /**
     * 更新检查表
     * @param checkGroup
     */
    @Update("update t_checkgroup set code=#{code},name=#{name},helpCode=#{helpCode},sex=#{sex},remark=#{remark},attention=#{attention} where id=#{id}")
    public void update(CheckGroup checkGroup);

    /**
     * 删除检查组数据
     * @param id
     */
    @Delete("delete from t_checkgroup where id=#{id}")
    public void delete(@Param("id") Integer id);

    /**
     * 查询checkGroup 所包含的 checkItem 的 id 用于更新数据时选项回显
     * @param id
     * @return
     */
    @Select("select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{id}")
    public List<Integer> findCheckItemIds(Integer id);

    /**
     * 查询检查组与套餐的关联个数
     * @param id
     * @return
     */
    @Select("select count(*) from t_setmeal_checkgroup where checkgroup_id=#{id}")
    public long findSetMeal(@Param("id") Integer id);

    /**
     * 按照制定规则去查询数据
     * @param checkitemIds
     * @return
     */
    @SelectProvider(type = CheckGroupProvider.class , method = "selectByOrder2")
    public List<CheckItem> getCheckItem(@Param("checkitemIds") Integer[] checkitemIds);

    @Select("select * from t_checkgroup where id in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{setmeal_id})")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "code",property = "code"),
            @Result(column = "name",property = "name"),
            @Result(column = "helpCode",property = "helpCode"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "remark",property = "remark"),
            @Result(column = "attention",property = "attention"),
            @Result(
                    column = "id",
                    property = "checkItems",
                    javaType = List.class,
                    many = @Many(select = "com.itheima.dao.CheckItemDao.findByInnerId")
            ),
    })
    public List<CheckGroup> findByInnerId(@Param("setmeal_id")Integer id);
}
